﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;

namespace CoolERP_Web.User
{
    public partial class CS_CPXL_Query : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AspNetPager1.PageSize = GridView1.PageSize;
                BindData();
            }
        }

        private void BindData()
        {
            using (CS_CPXLBLL cBLL = new CS_CPXLBLL())
            {
                int RecordCount = 0;
                List<vCPXL> itemLists = cBLL.GetCPXL(null, "CPXBH,BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            List<CS_CPXL> items = new List<CS_CPXL>();
            CS_CPXL item = new CS_CPXL();
            try
            {
                using (CS_CPXLBLL cBLL = new CS_CPXLBLL())
                {
                    foreach (GridViewRow container in GridView1.Rows)
                    {
                        HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                        if (cBox != null && cBox.Checked)
                        {
                            string[] str = cBox.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            item = cBLL.GetEntity(t => t.BH == str[0] && t.CPXBH == str[1]);
                            items.Add(item);
                        }
                    }
                    if (cBLL.Deletes(items))
                        ShowMessage("删除成功");
                }
            }
            catch (Exception ex)
            {
                this.ShowMessage("删除失败\n" + ex.Message);

            }
            finally
            {
                BindData();
            }
        }        
    }
}